package com.supermap.desktop.develop.workspacemigration.migration;

import com.supermap.data.*;
import com.supermap.desktop.develop.workspacemigration.internalhelpers.InternalHelper;

import java.util.ArrayList;
import java.util.HashMap;

public abstract class DBWorkspaceMigration extends WorkspaceMigration {

    private String srcIP;
    private String srcPort;
    private String srcDatabaseName;
    private String srcUserName;
    private String srcPassword;
    private EngineType srcEngineType;
    private String srcWorkspaceName;

    private String tarIP;
    private String tarPort;
    private String tarDatabaseName;
    private String tarUserName;
    private String tarPassword;
    private EngineType tarEngineType;

    private boolean isBatchMigration;

    protected HashMap<String, DatasourceConnectionInfo> migratedDatasources;

    protected DBWorkspaceMigration() {
        this("", "", "", "", "", EngineType.MEMORY, "",
                "", "", "", "", "", EngineType.MEMORY, false);
    }

    protected DBWorkspaceMigration(String srcIP, String srcPort, String srcDatabaseName,
                                   String srcUserName, String srcPassword, EngineType srcEngineType,
                                   String tarIP, String tarPort, String tarDatabaseName,
                                   String tarUserName, String tarPassword, EngineType tarEngineType, boolean isBatchMigration) {
        this(srcIP, srcPort, srcDatabaseName, srcUserName, srcPassword, srcEngineType, "",
                tarIP, tarPort, tarDatabaseName, tarUserName, tarPassword, tarEngineType, isBatchMigration);
    }

    protected DBWorkspaceMigration(String srcIP, String srcPort, String srcDatabaseName, String srcUserName,
                                   String srcPassword, EngineType srcEngineType, String srcWorkspaceName,
                                   String tarIP, String tarPort, String tarDatabaseName,
                                   String tarUserName, String tarPassword, EngineType tarEngineType, boolean isBatchMigration) {
        this.srcIP = srcIP;
        this.srcPort = srcPort;
        this.srcDatabaseName = srcDatabaseName;
        this.srcUserName = srcUserName;
        this.srcPassword = srcPassword;
        this.srcEngineType = srcEngineType;
        this.srcWorkspaceName = srcWorkspaceName;

        this.tarIP = tarIP;
        this.tarPort = tarPort;
        this.tarDatabaseName = tarDatabaseName;
        this.tarUserName = tarUserName;
        this.tarPassword = tarPassword;
        this.tarEngineType = tarEngineType;

        this.isBatchMigration = isBatchMigration;

        initWorkspaces();
    }

    public String getSrcIP() {
        return srcIP;
    }

    public void setSrcIP(String srcIP) {
        this.srcIP = srcIP;
    }

    public String getSrcPort() {
        return srcPort;
    }

    public void setSrcPort(String srcPort) {
        this.srcPort = srcPort;
    }

    public String getSrcDatabaseName() {
        return srcDatabaseName;
    }

    public void setSrcDatabaseName(String srcDatabaseName) {
        this.srcDatabaseName = srcDatabaseName;
    }

    public String getSrcUserName() {
        return srcUserName;
    }

    public void setSrcUserName(String srcUserName) {
        this.srcUserName = srcUserName;
    }

    public String getSrcPassword() {
        return srcPassword;
    }

    public void setSrcPassword(String srcPassword) {
        this.srcPassword = srcPassword;
    }

    public EngineType getSrcEngineType() {
        return srcEngineType;
    }

    public void setSrcEngineType(EngineType srcEngineType) {
        this.srcEngineType = srcEngineType;
    }

    public String getSrcWorkspaceName() {
        return srcWorkspaceName;
    }

    public void setSrcWorkspaceName(String srcWorkspaceName) {
        this.srcWorkspaceName = srcWorkspaceName;
    }

    public String getTarIP() {
        return tarIP;
    }

    public void setTarIP(String tarIP) {
        this.tarIP = tarIP;
    }

    public String getTarPort() {
        return tarPort;
    }

    public void setTarPort(String tarPort) {
        this.tarPort = tarPort;
    }

    public String getTarDatabaseName() {
        return tarDatabaseName;
    }

    public void setTarDatabaseName(String tarDatabaseName) {
        this.tarDatabaseName = tarDatabaseName;
    }

    public String getTarUserName() {
        return tarUserName;
    }

    public void setTarUserName(String tarUserName) {
        this.tarUserName = tarUserName;
    }

    public String getTarPassword() {
        return tarPassword;
    }

    public void setTarPassword(String tarPassword) {
        this.tarPassword = tarPassword;
    }

    public EngineType getTarEngineType() {
        return tarEngineType;
    }

    public void setTarEngineType(EngineType tarEngineType) {
        this.tarEngineType = tarEngineType;
    }

    public boolean isBatchMigration() {
        return isBatchMigration;
    }

    public void setBatchMigration(boolean batchMigration) {
        isBatchMigration = batchMigration;
    }

    @Override
    protected long getToMigratedDatasetsCount() {
        long totalDatasetsCount = 0;
        ArrayList<Workspace> srcWorkspaces = getSrcWorkspaces();
        EngineType srcEngineType = getSrcEngineType();
        ArrayList<String> datasourceAliasList = new ArrayList<>();
        for (Workspace srcWorkspace : srcWorkspaces) {
            Datasources datasources = srcWorkspace.getDatasources();
            for (int i = 0; i < datasources.getCount(); i++) {
                Datasource datasource = datasources.get(i);
                if (datasource.getEngineType() == srcEngineType) {
                    String alias = datasource.getAlias();
                    if (!datasourceAliasList.contains(alias)) {
                        totalDatasetsCount += datasource.getDatasets().getCount();
                        datasourceAliasList.add(alias);
                    }
                }
            }
        }
        datasourceAliasList.clear();
        return totalDatasetsCount;
    }

    protected ArrayList<String> querySrcWorkspaceNames() {
        return InternalHelper.queryWorkspaceNames(srcIP, srcPort, srcDatabaseName, srcUserName, srcPassword, srcEngineType);
    }
}
